var createError = require("http-errors");
var express = require("express");
const bodyParser = require("body-parser");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var followRouter = require("./routes/follow");

var app = express();

// swagger
// var swaggerUi = require("swagger-ui-express");
// import swaggerJSDoc from "swagger-jsdoc";

var http = require("http");
var server = http.createServer(app);

// view engine setup
// 模板
// app.set("views", path.join(__dirname, "views"));
// app.set("view engine", "jade");

// 日志
// app.use(logger("dev"));
// app.use(express.json());

app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

// 静态资源
app.use(express.static(path.join(__dirname, "public")));
// post
app.use(bodyParser.urlencoded({ extended: true }));

// 路由
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/follow", followRouter);

// catch 404 and forward to error handler
// app.use(function (req, res, next) {
//   next(createError(404));
// });

// // error handler
// app.use(function (err, req, res, next) {
//   // set locals, only providing error in development
//   res.locals.message = err.message;
//   res.locals.error = req.app.get("env") === "development" ? err : {};

//   // render the error page
//   res.status(err.status || 500);
//   res.render("error");
// });

// module.exports = app;

// var swaggerDefinition = {
//   info: {
//     title: "Node Swagger API",
//     version: "1.0.0",
//     description: "Swagger 接口文档",
//   },
//   host: "localhost:3022",
//   basePath: "/",
// };

// // options for the swagger docs
// var options = {
//   swaggerDefinition: swaggerDefinition,
//   // path to the API docs
//   apis: ["./routes/*.js"],
// };

// // initialize swagger-jsdoc
// var swaggerSpec = swaggerJSDoc(options);
// console.log("swaggerSpec", swaggerSpec);

// // serve swagger
// app.get("/swagger.json", function (req, res) {
//   res.setHeader("Content-Type", "application/json");
//   res.send(swaggerSpec);
// });

// app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));

server.listen("3022");
console.log("server runing http://localhost:3022/");
